﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Imaging;

namespace AnaViewer
{
    public partial class Common
    {
        #region 嵌入桌面
        [DllImport("User32.dll", EntryPoint = "FindWindow")]
        private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
        [DllImport("User32.dll", EntryPoint = "SetParent")]
        private static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);

        /// <summary>
        /// 将窗口嵌入到桌面
        /// </summary>
        /// <param name="isEmbeded">是否嵌入.if set to <c>true</c> [is embeded].</param>
        public static void EmbedDesktop(Form form, bool isEmbeded)
        {
            if (isEmbeded)
            {
                IntPtr hDeskTop = FindWindow("Progman", "Program Manager");
                SetParent(form.Handle, hDeskTop);
            }
            else
            {
                SetParent(form.Handle, IntPtr.Zero);// this.Handle);
            }
        }
        #endregion

        #region 鼠标穿透

        private const uint WS_EX_LAYERED = 0x80000;
        private const int WS_EX_TRANSPARENT = 0x20;
        private const int GWL_STYLE = (-16);
        private const int GWL_EXSTYLE = (-20);
        private const int LWA_ALPHA = 0x2;

        [DllImport("user32", EntryPoint = "SetWindowLong")]
        private static extern uint SetWindowLong(IntPtr hwnd, int nIndex, uint dwNewLong);

        [DllImport("user32", EntryPoint = "GetWindowLong")]
        private static extern uint GetWindowLong(IntPtr hwnd, int nIndex);

        [DllImport("user32", EntryPoint = "SetLayeredWindowAttributes")]
        private static extern int SetLayeredWindowAttributes(IntPtr hwnd, int crKey, int bAlpha, int dwFlags);

        /// <summary>
        /// 使窗口有鼠标穿透功能
        /// </summary>
        public static void CanPenetrate(Form form, bool canPenetrate)
        {
            if (canPenetrate)
            {//使窗口有鼠标穿透功能
                uint intExTemp = GetWindowLong(form.Handle, GWL_EXSTYLE);
                uint oldGWLEx = SetWindowLong(form.Handle, GWL_EXSTYLE, intExTemp | WS_EX_TRANSPARENT | WS_EX_LAYERED);
                //SetLayeredWindowAttributes(form.Handle, 0, 100, LWA_ALPHA);
            }
            else
            {//使窗体恢复正常
                form.FormBorderStyle = FormBorderStyle.None;
            }
        }
        #endregion

        #region PerPixelAlpha 透明窗体


        /// <summary>
        /// 使窗口具有 WS_EX_LAYERED 扩展样式。
        /// CreateParams.ExStyle |= 0x00080000; // This form has to have the WS_EX_LAYERED extended style
        /// </summary>
        public static void SetAlphaStyle(Form form, bool isAlpha)
        {
            if (isAlpha)
            {
                uint intExTemp = GetWindowLong(form.Handle, GWL_EXSTYLE);
                uint oldGWLEx = SetWindowLong(form.Handle, GWL_EXSTYLE, intExTemp | WS_EX_LAYERED);
                //SetLayeredWindowAttributes(form.Handle, 0, 100, LWA_ALPHA);
            }
            else
            {//使窗体恢复正常
                form.FormBorderStyle = FormBorderStyle.None;
            }
        }





        /// <para>Changes the current bitmap.</para>
        public static void SetBitmap(Form form, Bitmap bitmap)
        {
            SetBitmap(form,bitmap, 255);
        }


        /// <para>Changes the current bitmap with a custom opacity level.  Here is where all happens!</para>
        public static void SetBitmap(Form form, Bitmap bitmap, byte opacity)
        {
            if (bitmap.PixelFormat != PixelFormat.Format32bppArgb)
                throw new ApplicationException("The bitmap must be 32ppp with alpha-channel.");

            // The ideia of this is very simple,
            // 1. Create a compatible DC with screen;
            // 2. Select the bitmap with 32bpp with alpha-channel in the compatible DC;
            // 3. Call the UpdateLayeredWindow.

            IntPtr screenDc = Win32.GetDC(IntPtr.Zero);
            IntPtr memDc = Win32.CreateCompatibleDC(screenDc);
            IntPtr hBitmap = IntPtr.Zero;
            IntPtr oldBitmap = IntPtr.Zero;

            try
            {
                hBitmap = bitmap.GetHbitmap(Color.FromArgb(0));  // grab a GDI handle from this GDI+ bitmap
                oldBitmap = Win32.SelectObject(memDc, hBitmap);

                Win32.Size size = new Win32.Size(bitmap.Width, bitmap.Height);
                Win32.Point pointSource = new Win32.Point(0, 0);
                Win32.Point topPos = new Win32.Point(form.Left, form.Top);
                Win32.BLENDFUNCTION blend = new Win32.BLENDFUNCTION();
                blend.BlendOp = Win32.AC_SRC_OVER;
                blend.BlendFlags = 0;
                blend.SourceConstantAlpha = opacity;
                blend.AlphaFormat = Win32.AC_SRC_ALPHA;

                Win32.UpdateLayeredWindow(form.Handle, screenDc, ref topPos, ref size, memDc, ref pointSource, 0, ref blend, Win32.ULW_ALPHA);
            }
            finally
            {
                Win32.ReleaseDC(IntPtr.Zero, screenDc);
                if (hBitmap != IntPtr.Zero)
                {
                    Win32.SelectObject(memDc, oldBitmap);
                    //Windows.DeleteObject(hBitmap); // The documentation says that we have to use the Windows.DeleteObject... but since there is no such method I use the normal DeleteObject from Win32 GDI and it's working fine without any resource leak.
                    Win32.DeleteObject(hBitmap);
                }
                Win32.DeleteDC(memDc);
            }
        }
        #endregion
    }
}
